<template>
	<view class="navbar">
		<view class="navbar-fixed">
			<!-- 状态栏 -->
			<!-- #ifndef MP-ALIPAY -->
				<view :style="{height: statusBarHeight + 'px'}"></view>
			<!-- #endif -->
			<!-- 导航栏 -->
			<view :class="['navbar-content',isSearch ? 'search' : '']" :style="{height: navBarHeight + 'px', width: windowWidth + 'px'}"
			 @click.stop="openSearchPage">
				<view class="navbar-content_search-icons" @click="back" v-if="isSearch">
					<uni-icons type="back" size="22" color="#fff"></uni-icons>
				</view>
				<view class="navbar-search">
					<view class="navbar-search__icon">
						<uni-icons type="search" size="16" color="#999"></uni-icons>
					</view>
					<!-- 非搜索页面显示 -->
					<view class="navbar-search__text" v-if="!isSearch">
						search
					</view>
					<!-- 搜索页面显示 -->
					<template v-else>
						<input class="navbar-search__text" type="text" placeholder="请输入您要搜索的内容" v-model="value" @confirm="confirm" />
					</template>
				</view>
			</view>
		</view>
		<view :style="{height: navBarHeight+statusBarHeight  + 'px'}"></view>
	</view>
</template>

<script>
	import '@/common/css/icons.css'
	export default {
		props: {
			isSearch: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				// 手机状态栏的高度
				statusBarHeight: 20,
				navBarHeight: 45,
				windowWidth: 375,
				// 搜索的内容
				value: ''
			}
		},
		created() {
			// 获取手机系统信息
			const info = uni.getSystemInfoSync()
			this.statusBarHeight = info.statusBarHeight
			this.windowWidth = info.windowWidth
			// #ifndef H5 || APP-PLUS || MP-ALIPAY
			// 获取胶囊的位置信息
			const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			// 导航栏的高度计算
			// （胶囊底部高度 - 状态栏的高度） + （胶囊顶部高度 - 状态栏的高度）
			this.navBarHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (menuButtonInfo.top - info.statusBarHeight)
			// 搜索宽度
			this.windowWidth = menuButtonInfo.left
			// #endif	

			// 自定义事件
			uni.$on('historySearch', (value) => {
				this.value = value
			})
			
			// #ifdef MP-ALIPAY
			this.statusBarHeight = 0
			// #endif

		},
		methods: {
			// 打开搜索页面
			openSearchPage() {

				// 判断是否在搜索页! 是则不向下执行
				if (this.isSearch) return

				uni.navigateTo({
					url: '/pages/home-search/home-search'
				})

			},
			// 返回上一级路由
			back() {
				uni.switchTab({
					url: '/pages/tabbar/index/index'
				})
			},
			// 确定搜索
			confirm() {
				this.$emit('confirm', this.value)
			}
		}
	}
</script>

<style lang="scss">
	.navbar {
		.navbar-fixed {
			width: 100%;
			background-color: $mk-base-color;
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
			z-index: 99;

			.navbar-content {
				height: 90upx;
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 0 30upx;
				box-sizing: border-box;

				.navbar-search {
					width: 100%;
					height: 60upx;
					background-color: #fff;
					display: flex;
					align-items: center;
					border-radius: 60upx;
					padding: 0 20upx;
					box-sizing: border-box;

					.navbar-search__icon {
						margin-right: 20upx;
					}

					.navbar-search__text {
						font-size: 28upx;
						color: #999;
						width: 100%;
						padding-right: 50upx;
					}
				}

				&.search {
					padding-left: 0;

					.navbar-content_search-icons {
						margin-left: 20upx;
						margin-right: 20upx;
					}

					.navbar-search {
						border-radius: 10upx;
					}
				}
			}
		}
	}
</style>
